require([
	'jquery',
	'module/util',
	'module/popup',
	'global',
	'datatables',
	'jquery.debounce',
	'bootstrap',
	'requirejs/domready!'],
function($, util, popup, g){
	var article_table = null,
	    catalogId = $('#catalog-id-marker').data('id');
	
	$('.left-sidebar > div, .right-sidebar > div').affix({
	    offset: {
		    top: 120,
		    bottom: 20
	    }
	});
	
	if(catalogId){
		// 渲染表格
		$('#article-table').on('click', 'tbody td', function(){
			var dt = $('#article-table').dataTable().api();
			var idx = dt.cell(this).index();
			if(!idx) return;
			var row = dt.row(idx.row).data();
		}).on('draw.dt', function(){
	    	if(article_table){
	    		article_table.columns.adjust();
	    	}
	    }).dataTable( {
	    	language: {
	            url: g.context + '/assets/js/chinese.json'
	        },
	        dom: 'rt<"row"<"col-md-4"i><"col-md-8"p>>',
	    	processing : true,
		    serverSide : true,
		    scrollX: '100%',
		    ajax: {
		    	type: 'POST',
		    	url: g.context + '/catalogs/'+catalogId+'/articles',
		    	contentType: 'application/json; charset=utf-8',
		    	global: false,
		    	data: function(d){
		    		return JSON.stringify(d);
		    	},
		    	error: function(xhr, status, error){
		    		console.log(error);
		    	}
		    },
		    columnDefs: [ 
		     	{ targets : ['_all'], defaultContent: '&nbsp;', searchable : false }
		    ],
		    columns: [
		        {   
		        	data: 'title',
		        	render : function ( data, type, row ) {
					    return '<a href="'+g.context+'/article/'+row.id+'">'+row.title+'</a>';
					}
		        },
		        { data: 'createdAt'}
		    ]
	    });
		article_table = $('#article-table').dataTable().api();
	}
});